 // pages/series/series.js
var that,
  api = new (require('../../utils/api.js')),
  headimg=api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js')); 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    nocart:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    let cartids = wx.getStorageSync("cartids");
    this.setData({ cartids });
    if (options.i&&options.t){
      this.setData({type:options.t,id:options.i});
      this.getmaster(options.t, options.i);
    }
    
  },
  getmaster(type,id){
    wx.showLoading({
      title: '加载中',
    })
    if(type==0){
      // wx.setNavigationBarTitle({
      //   title: '名家大师',
      // })
      api.getmaster({id},res=>{
        if(res.data.issuccess==1){
          let target = res.data.data;
    
          if(target.thumbpath.indexOf("http")==-1){
            target.thumbpath=headimg+target.thumbpath;
          }
            this.setData({target});
            this.findbooksbyids(target.goodsid);
        }
      });
    }else if(type==1){
      // wx.setNavigationBarTitle({
      //   title: '大奖作品',
      // })
      api.getaward({ id }, res => {
        if (res.data.issuccess == 1) {
          let target = res.data.data;

          if (target.thumbpath.indexOf("http") == -1) {
            target.thumbpath = headimg + target.thumbpath;
          }
          this.setData({ target });
          this.findbooksbyids(target.goodsid);
        }
      });
    }
  },
  addmorecart(){
    let {nocart,list} = this.data,
    ids = nocart.join(",");
    if(nocart.length==0){
      wx.showToast({
        icon:'none',
        title: '不要重复添加',
      })
      return;
    }
    list.forEach((x,i)=>{
      x.already = !0;
    });
 
    api.addmorecart({ids},res=>{
      if(res.data.issuccess==1){
        this.setData({nocart:[],list});
        wx.showToast({
          title: '加入成功',
        })
        let cartids = wx.getStorageSync("cartids");

          list.forEach((x, i) => {
            if (cartids.indexOf(x.id) > -1) {
              return;
            } else {
              cartids.push(x.id);
            }
          });
        console.log(cartids,'cartids')
        wx.setStorageSync("cartids", cartids)
        app.badege(cartids);
      }
    });
  },
  editcart(e) {
    console.log(211111111);
    
    let { index } = e.currentTarget.dataset, { list, cartids,nocart } = this.data,type='BOOKS',
      already = !list[index].already,
      ids = list[index].id;

    if (already) {
      api.addcart({ goodsid: ids ,type}, res => {
        if (res.data.issuccess == 1) {
          list[index].already = already;
          wx.showToast({
            title: '添加成功',
          })
          cartids.push(ids);
          let i = nocart.indexOf(ids);
          
          nocart.splice(i,1);
          
          this.setData({ list, cartids });
          wx.setStorageSync("cartids", cartids);
          app.badege(cartids);
        }
      });
    } else {
      api.delcart({ ids }, res => {
        if (res.data.issuccess == 1) {
          list[index].already = already;
          wx.showToast({
            title: '取消成功',
          })
          let i = cartids.indexOf(ids);
          cartids.splice(i, 1);
          nocart.push(ids);
          this.setData({ list, cartids });
          wx.setStorageSync("cartids", cartids);
          app.badege(cartids);
        }

      })
    }
  },

  findbooksbyids(ids){
    api.getbooks({ids},res=>{
      if(res.data.issuccess==1){
        this.formatdata(res.data.data);
      }
    });
  },
  formatdata(list) {
    let {cartids,nocart} = this.data;
    list.forEach((x, i) => {
      if (x.thumbPicPath.indexOf("http") == -1) {
        x.thumbPicPath = headimg + x.thumbPicPath;
      }
      if (cartids.indexOf(x.id) != -1) {
        list[i].already = !0;
      }else{
        nocart.push(x.id);
      }
      // x.detaillist = x.detaillist.split(',');
      x.labels = x.labels.split(',');
      // if(x.detaillist.length>0){
      //   x.detaillist.forEach((y, i) => {
      //     if (y.indexOf("http") == -1) {
      //       x.detaillist[i] = headimg + x.detaillist[i];
      //     }
      //   });
      // }
    });
    this.setData({list});
    wx.hideLoading();
  },
  todetail(e){
    let {id} = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../books/detail/detail?i='+id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
 
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.logininfo.appletname,
      path: `pages/listen/listen?share=`+app.logininfo.userid,
    }
  }
})